<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>数据清除</span>
        <span
          style="float: right; padding: 3px 0; color: red; font-size: 12px"
          type="text"
          >注意事项：默认可清除一周内的数据(可修改配置)
        </span>
      </div>

      <el-form
        :inline="true"
        ref="queryForm"
        :model="queryParams"
        label-width="68px"
      >
        <el-form-item label="提报人" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入人员"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select
            v-model="queryParams.state"
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in stateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="resList" stripe>
        <el-table-column
          label="填报编号"
          align="center"
          width="80"
          prop="fillId"
        />
        <el-table-column
          label="提报日期"
          align="center"
          prop="workTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.workTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="提报人" prop="nickName" />
        <el-table-column label="部门" width="120" prop="deptName" />
        <el-table-column
          label="项目代码"
          width="150"
          prop="projectCode"
          show-overflow-tooltip
        />
        <el-table-column label="开发阶段" prop="stageName" />
        <el-table-column label="事务种类" width="230" prop="officeName" />
        <el-table-column
          label="提报工时(H)"
          width="120"
          align="center"
          prop="workHour"
        />
        <el-table-column
          label="描述"
          prop="workRemark"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="回退描述"
          prop="rollback"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column label="审核人" prop="checkBy" />
        <el-table-column label="状态" align="center" prop="state">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.state === '已审核'
                  ? 'primary'
                  : scope.row.state === '未审核'
                  ? 'info'
                  : scope.row.state === '回退'
                  ? 'warning'
                  : 'danger'
              "
              disable-transitions
              >{{ scope.row.state }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-hasPermi="['work:clear:list']"
              @click="handleDelete(scope.row)"
              >数据清除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 回退 -->
    <el-dialog
      :title="clearTitle"
      :visible.sync="isClear"
      width="500px"
      append-to-body
    >
      <el-form
        ref="clearForm"
        :model="clearForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="删除原因" prop="clearDescription">
          <el-input
            type="textarea"
            :rows="3"
            v-model="clearForm.clearDescription"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clearSubmitForm">确 定</el-button>
        <el-button @click="clearCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, clear } from '@/api/work/clear'

export default {
  name: 'DataClear',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: '',
        state: ''
      },
      // 数据列表
      resList: [],
      // 总条数
      total: 0,
      // 状态选择
      stateOptions: ['未审核', '已审核', '回退', '未提报'],
      // 弹出层标题
      clearTitle: '',
      // 是否显示弹出层
      isClear: false,
      // 清除表单参数
      clearForm: {},
      // 表单校验
      rules: {
        clearDescription: [
          { required: true, message: '删除原因不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 初始化 */
    getList() {
      this.loading = true
      list(this.queryParams).then((response) => {
        this.resList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        nickName: '',
        state: ''
      }
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fillId = row.fillId
      console.log('删除：' + fillId)
      this.clearForm = {}
      this.resetForm('clearForm')
      let copy = Object.assign({}, row) //row是所编辑本行内数据对象
      this.clearForm = copy
      this.isClear = true
      this.clearTitle = '清除填报编号' + this.clearForm.fillId + '数据'
    },
    /** 确定 */
    clearSubmitForm() {
      if (this.clearForm) {
        this.$refs['clearForm'].validate((valid) => {
          if (valid) {
            this.clearForm.params = undefined
            clear(this.clearForm).then((response) => {
              this.$modal.msgSuccess('清除成功')
              this.isClear = false
              this.getList()
            })
          }
        })
      } else {
        this.$message.error('数据异常')
      }
    },
    /** 取消 */
    clearCancel() {
      this.isClear = false
      this.clearForm = {}
    }
  }
}
</script>
